import 'package:flutter/material.dart';
import 'package:my_app/conponate/user_info.dart';
import 'package:my_app/tools/http_server.dart';
import 'login.dart';

class ChangePasswardPage extends StatefulWidget {
  const ChangePasswardPage({Key? key}) : super(key: key);

  @override
  _ChangePasswardPageState createState() => _ChangePasswardPageState();
}

class _ChangePasswardPageState extends State<ChangePasswardPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(

      appBar: AppBar(
        backgroundColor: Colors.blueGrey,
        title: Text(
          "修改密码",
        ),

        centerTitle: true,
        elevation: 5.0,
        // 阴影
      ),
      body: FromofLogin(),
    );
  }
}

class FromofLogin extends StatefulWidget {
  const FromofLogin({Key? key}) : super(key: key);

  @override
  _FromofLoginState createState() => _FromofLoginState();
}

class _FromofLoginState extends State<FromofLogin> {
  GlobalKey _globalKey = GlobalKey<FormState>();
  TextEditingController _textEditingController01 = TextEditingController();
  TextEditingController _textEditingController001 = TextEditingController();
  TextEditingController _textEditingController02 = TextEditingController();
  TextEditingController _textEditingController03 = TextEditingController();
  FocusNode _username_focus = FocusNode();
  FocusNode _gen_passward_focus = FocusNode();
  FocusNode _password_focus = FocusNode();
  FocusNode _parepeat_focus = FocusNode();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _textEditingController01.dispose();
    _textEditingController02.dispose();
    _textEditingController03.dispose();
    _username_focus.dispose();
    _password_focus.dispose();
    _parepeat_focus.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: EdgeInsets.fromLTRB(40, 0, 40, 0),
        child: Form(
          key: _globalKey,
          child: Column(
            children: [
              SizedBox(height: 50),
              TextFormField(
                autofocus: true,
                focusNode: _username_focus,
                controller: _textEditingController01,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.person,color:Colors.blueGrey,),
                  labelText: "用户名",
                  hintText: "username",
                  labelStyle: TextStyle(
                    color: Colors.blueGrey,),
                  hintStyle: TextStyle(
                    color: Colors.blueGrey,),
                  focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.blueGrey),
                  ),
                ),
                textInputAction: TextInputAction.next,
                onFieldSubmitted: (v) {},
              ),
              SizedBox(height: 8),
              TextFormField(
                focusNode: _gen_passward_focus,
                controller: _textEditingController001,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.password,color:Colors.blueGrey,),
                  labelText: "旧密码",
                  hintText: "password",
                  labelStyle: TextStyle(
                    color: Colors.blueGrey,),
                  hintStyle: TextStyle(
                    color: Colors.blueGrey,),
                  focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.blueGrey),
                  ),
                ),
                obscureText: true,
                textInputAction: TextInputAction.next,
              ),
              SizedBox(height: 8),
              TextFormField(
                focusNode: _password_focus,
                controller: _textEditingController02,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.password,color:Colors.blueGrey,),
                  labelText: "新密码",
                  hintText: "new password",
                  labelStyle: TextStyle(
                    color: Colors.blueGrey,),
                  hintStyle: TextStyle(
                    color: Colors.blueGrey,),
                  focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.blueGrey),
                  ),
                ),
                obscureText: true,
                textInputAction: TextInputAction.next,
              ),
              SizedBox(height: 8),
              TextFormField(
                focusNode: _parepeat_focus,
                controller: _textEditingController03,
                autovalidate: true,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.password,color:Colors.blueGrey,),
                  labelText: "再次输入",
                  hintText: "new password",
                  labelStyle: TextStyle(
                    color: Colors.blueGrey,),
                  hintStyle: TextStyle(
                    color: Colors.blueGrey,),
                  focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.blueGrey),
                  ),
                ),
                obscureText: true,
                textInputAction: TextInputAction.send,
                validator: (v) {
                  if (_textEditingController02.text !=
                      _textEditingController03.text) {
                    return "两次密码输入不一致！！";
                  }
                },
              ),
              // SizedBox(height: 10),
              // GestureDetector(
              //   child: Container(
              //     width: 420,
              //     child: Text(
              //       "No account? Register here.",
              //       textAlign: TextAlign.right,
              //       style: TextStyle(
              //         color: const Color(0xff414833),
              //       ),
              //     ),
              //   ),
              //   onTap: () {
              //     print("Go to the register page");
              //   },
              // ),
              SizedBox(height: 30),
              RaisedButton(
                onPressed: () async {
                  print("username:" + _textEditingController01.text);
                  print("gene_pass" + _textEditingController001.text);
                  print("password:" + _textEditingController02.text);
                  print("password repeat:" + _textEditingController03.text);
                  // if((_globalKey.currentState as FormState ).validate().toString() == true){
                  //   HttpServer.signUp(_textEditingController01.text, _textEditingController02.text);}
                  // print((_globalKey.currentState as FormState)
                  //     .validate()
                  //     .toString());
                  String responses = await HttpServer.changePassword(_textEditingController01.text, _textEditingController001.text, _textEditingController02.text);
                  print("???????????????????" + responses);
                  if(responses == "Failed"){
                    print("密码错误");
                  } else{
                    print("!!!!!!!!!!!!!!!!!!!!!!");
                    Navigator.of(context).push(
                      MaterialPageRoute(
                        builder: (context) {
                          return LoginNameAndPassword();
                        },
                      ),
                    );
                  }
                },
                child: Text(
                  "提交",
                  style:
                      TextStyle(fontSize: 20, color: Colors.blueGrey.shade100),
                ),
                color: Colors.blueGrey,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                  side: BorderSide(
                    color: Colors.blueGrey.shade700,
                    width: 2,
                  ),
                ),
              )
            ],
          ),
        ));
  }
}
